package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.global.AppGlobals
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GroupBookingLuckyPopBean
import com.example.wallet.db.UserInfoManager
import com.lxj.xpopup.core.CenterPopupView

class GroupBookingLuckyPop(context: Context) : CenterPopupView(context) {

    private var gbid = ""

    fun setGbid(gbid: String) {
        this.gbid = gbid
    }

    private lateinit var rewarding: TextView

    private lateinit var rewardingContent: TextView

    private lateinit var rewardingValue: TextView

    private lateinit var rewardingExtra: TextView

    private lateinit var rewardingTime: TextView

    private lateinit var rewardingList: RecyclerView

    private val cml = AppGlobals.getApplication().getString(R.string.wallet_cml)

    private var mAdapter: JssBaseQuickAdapter<GroupBookingLuckyPopBean.GbLogListBean>? = null

    override fun getImplLayoutId(): Int {
        return R.layout.group_booking_lucky_pop
    }

    override fun onCreate() {
        super.onCreate()

        rewarding = findViewById(R.id.rewarding)

        rewardingContent = findViewById(R.id.rewardingContent)

        rewardingValue = findViewById(R.id.rewardingValue)

        rewardingExtra = findViewById(R.id.rewardingExtra)

        rewardingTime = findViewById(R.id.rewardingTime)

        rewardingList = findViewById(R.id.rewardingList)

        rewardingList.setHasFixedSize(true)

        rewardingList.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

        mAdapter = object : JssBaseQuickAdapter<GroupBookingLuckyPopBean.GbLogListBean>(
            R.layout.group_booking_lucky_item_pop
        ) {
            override fun convert(
                holder: JssBaseViewHolder?,
                item: GroupBookingLuckyPopBean.GbLogListBean?
            ) {
                val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
                val textColor = if ((item?.uid ?: "") == ( userInfo?.uid ?:"-90"))
                    R.color.text_color_13 else R.color.text_color_1
                holder?.setText(R.id.list_item, "${item?.nickName} (${item?.account})")
                    ?.setTextColorRes(R.id.list_item,textColor)
            }
        }

        rewardingList.adapter=mAdapter

        findViewById<View>(R.id.closeBt).setOnClickListener {
            dismiss()
        }
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo

        val map = mapOf(
            "gbid" to gbid,
            "uid" to userInfo?.uid
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.groupBuyResult)
            .success {
                parseGroupBuyResult(it)
            }.error { _, msg ->
                Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
                dismiss()
            }.failure {
                Toast.makeText(context, "获取结果失败", Toast.LENGTH_LONG).show()
                dismiss()
            }.build().post()

    }

    @SuppressLint("SetTextI18n")
    private fun parseGroupBuyResult(it: String?) {
        val obj = JSON.parseObject(it, GroupBookingLuckyPopBean::class.java)
        mAdapter?.clears()
        mAdapter?.replaceData(obj?.gbLogList?: emptyList())

        rewarding.text = "${obj?.winNickName} (${obj?.winAccount})"
        rewardingContent.text = "获得：${obj?.goodsName} +"
        rewardingValue.text = "${obj?.tokenNums} $cml"

        rewardingTime.text = "成团时间：${obj?.lastDate}"

        rewardingExtra.text = "${obj?.backDesc}"


    }

}